package com.harmontronics.hanma.paas.paas.domain.app.entity.valueobject;

/**
 * 数据类型
 *
 * @author 吕大兵
 */
public enum EntityModelTypeEnum {
    /**
     * 短文本
     */
    SHORT_TEXT(Fields.VARCHAR, 50, Fields.MAX_LENGTH),
    /**
     * 长文本
     */
    LONG_TEXT(Fields.TEXT, 0, Fields.MAX_LENGTH),
    /**
     * 数值
     */
    NUMBER(Fields.NUMERIC, 20, Fields.MAX_LENGTH),
    /**
     * 日期
     */
    DATE(Fields.INT8, 0, 0),
    /**
     * 时间
     */
    TIME(Fields.VARCHAR, 20, 100),
    /**
     * 图片
     */
    PICTURE(Fields.VARCHAR, 300, Fields.MAX_LENGTH),
    /**
     * 附件
     */
    ATTACHMENT(Fields.VARCHAR, 300, Fields.MAX_LENGTH),
    /**
     * 人员
     */
    STAFF(Fields.VARCHAR, 64, Fields.MAX_LENGTH),
    /**
     * 部门
     */
    DEPARTMENT(Fields.VARCHAR, 500, Fields.MAX_LENGTH),
    /**
     * 关联选择
     */
    RELATION_SELECT(Fields.VARCHAR, 500, 0),
    /**
     * 子表
     */
    SUB_TABLE(Fields.INT8, 0, 0),
    /**
     * 关联主键
     */
    RELATION_PRIMARY_KEY(Fields.INT8, 0, 0),
    /**
     * 父节点
     */
    PARENT_ID(Fields.VARCHAR, 100, Fields.MAX_LENGTH),
    /**
     * 父节点集合
     */
    PARENT_IDS(Fields.VARCHAR, 255, Fields.MAX_LENGTH),
    /**
     * 子节点
     */
    CHILDREN(Fields.VARCHAR, 50, Fields.MAX_LENGTH),
    ;


    private String type;

    private int defLength;

    private int maxLength;

    EntityModelTypeEnum(String type, int defLength, int maxLength) {
        this.type = type;
        this.defLength = defLength;
        this.maxLength = maxLength;
    }

    /**
     * 全部可空
     * @param code
     * @param length
     * @param nullable
     * @return
     */
    public String getColumnDef(String code, int length, boolean nullable) {
        switch (this) {
            case SHORT_TEXT:
            case PICTURE:
            case ATTACHMENT:
            case STAFF:
            case DEPARTMENT:
            case RELATION_SELECT:
            case PARENT_ID:
            case PARENT_IDS:
            case CHILDREN:
            case TIME:
                return code + " " + this.type + "(" + (length == 0 ? this.defLength : length) + ")" ;
            case NUMBER:
                return code + " " + this.type + "(" + this.defLength + ",6)";
            case LONG_TEXT:
            case DATE:
            case SUB_TABLE:
            case RELATION_PRIMARY_KEY:
                return code + " " + this.type;
            default:
                return null;

        }
    }

    public boolean isVarCharType() {
        return this == SHORT_TEXT || this == PICTURE || this == ATTACHMENT || this == STAFF || this == DEPARTMENT;
    }
}
